package com.xunji.xunji.module.account.ui.fragment;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.huanxiao.base.base.BaseFragment;
import com.huanxiao.util.GlideHelper;
import com.xunji.xunji.R;

import me.crosswall.lib.coverflow.CoverFlow;
import me.crosswall.lib.coverflow.core.PagerContainer;

/**
 * Created by Paul on 2017/2/18.
 */

public class ScrollerFragment extends BaseFragment {
    String[] images = {
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1487410186719&di=4aff9bbf838fdc8fef2ffb1d860fb291&imgtype=0&src=http%3A%2F%2Fpic2.cxtuku.com%2F00%2F02%2F31%2Fb945758fd74d.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1487409713509&di=2944693dcf79d66500742432ae7961e0&imgtype=0&src=http%3A%2F%2Fi4.piimg.com%2F11340%2F7f638e192b9079e6.jpg",
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1487410186718&di=0967caf7b86510d77bc8b13b14152c2d&imgtype=0&src=http%3A%2F%2Fpic41.nipic.com%2F20140518%2F18521768_133448822000_2.jpg"};


    public static ScrollerFragment newInstance() {

        Bundle args = new Bundle();

        ScrollerFragment fragment = new ScrollerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutID() {
        return R.layout.fragment_scroller;
    }

    @Override
    public void initView(View view) {
        PagerContainer mContainer = (PagerContainer) view.findViewById(R.id.pager_container);

        final ViewPager pager = mContainer.getViewPager();

        PagerAdapter adapter = new MyPagerAdapter(images);
        pager.setAdapter(adapter);

        pager.setOffscreenPageLimit(adapter.getCount());

        pager.setClipChildren(false);

//        mContainer.setPageItemClickListener(new PageItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//                Toast.makeText(mActivity, "position:" + position, Toast.LENGTH_SHORT).show();
//            }
//        });


//        boolean showRotate = getIntent().getBooleanExtra("showRotate",true);
//
//        if(showRotate){
        new CoverFlow.Builder()
                .with(pager)
                .scale(0.3f)
                .pagerMargin(0f)
                .spaceSize(0f)
                .rotationY(25f)
                .build();
//        }
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initBundleExtras(Bundle extras) {

    }

    @Override
    public void initListener() {

    }

    @Override
    public boolean isBindEventBus() {
        return false;
    }

    private class MyPagerAdapter extends PagerAdapter {
        private String[] urls;

        public MyPagerAdapter(String[] urls) {
            this.urls = urls;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            final ImageView view = new ImageView(mActivity);
            GlideHelper.display(urls[position], view);
            container.addView(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getCount() {
            return urls.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return (view == object);
        }
    }

}
